import api from "../../config/api";
import request from "../../utils/request";
import {
  getUserLocation,
  getUserPermissions,
  shareOptions
} from "../../utils/access";
import {
  getXsScreen,
  jump,
  getStorage,
  setStorage
} from "../../utils/utils";
const app = getApp()
// pages/convenience/convenience.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    secondHandData: [],
    latitude: "",
    longitude: "",
    // 跳转代代小程序节流阀开关
    loading: false,
    // 是否是小屏幕尺寸
    ...getXsScreen(),
  },

  // 获取二手闲置最热数据
  async getSecondHandData() {
    // const lat = getStorage("latitude") === '' ? '34.805929169515046' : getStorage("latitude");
    // const lon = getStorage("longitude") === '' ? '113.66717101468758' : getStorage("longitude");
    // const {
    //   province,
    //   city,
    //   direct
    // } = getStorage('addressInfo') || {}

    // const {
    //   data
    // } = await request.get(api.allGoods, {
    //   type: 2,
    //   lat,
    //   lon,
    //   size: 6,
    //   provinceName: province || '',
    //   cityName: city || '',
    //   districtName: direct || ''
    // }, {
    //   loading: false
    // })
    // console.log(data)
    // this.setData({
    //   secondHandData: data?.records
    // })
    let params = {
      pageIndex: 1,
      sortType: 10,
      pageSize: 6,
      type: 1
    }
    let res = await request.get(api.miniGoodsPage, params,{ loading: false})
    if (res.httpStatus == 200) {
      this.setData({
        secondHandData: res?.data?.records
      })
    }
  },
  // 前往小鹿闲置首页
  goShopIndex(e) {
    if (app.isLogin()) {
      // // 判断定位权限是否打开
      // if (getUserLocation()) {
      //   jump('idleHome')
      // } else {
      //   getUserPermissions('userLocation', (res) => {
      //     if (res) {
      //       jump('idleHome')
      //     }
      //   })
      // }
      this.jumpMini()
    } else {
      app.login()
    }
  },
  // 前往小鹿闲置首页
  goShop(e) {
    if (app.isLogin()) {
      // wx.navigateTo({
      //   url: `/pages/idleGosDetail/idleGosDetail?goodsId=${e.currentTarget.dataset.id}&userId=${e.currentTarget.dataset.userid}`,
      // })
      this.jumpMini(e)
    } else {
      app.login()
    }
  },
  // 跳转代代小程序
  go(e) {
    if (app.isLogin()) {
      // 判断定位权限是否打开
      if (getUserLocation()) {
        wx.navigateTo({
          url: e.currentTarget.dataset.path,
        })
      } else {
        getUserPermissions('userLocation', (res) => {
          if (res) {
            wx.navigateTo({
              url: e.currentTarget.dataset.path,
            })
          }
        })
      }
    } else {
      app.login()
    }
  },
  // 跳转小鹿闲置小程序
  jumpMini(e) {
    let id = e?.currentTarget?.dataset?.id
    let path = ''
    if(id){
      path = `pages/goodsDetails/goodsDetails?source=QD000001&goodsId=${id}`
    }
    const {
      loadMiniProgram
    } = this.data
    if (loadMiniProgram) return
    this.setData({
      loadMiniProgram: true,
    })
    this.miniGoodsDataLogSave(2)
    //不是代代回收用户，跳转代代回收小程序完成注册
    wx.navigateToMiniProgram({
      appId: 'wx5a2f319913b5b3e9',
      path,
      envVersion: 'trial',
      success: () => {
        console.log('跳转成功')
        this.miniGoodsDataLogSave(3)
      },
      fail: (error) => {
        console.log('跳转失败', error)
        if (error?.errMsg !== 'navigateToMiniProgram:fail cancel') {
          wx.showToast({
            title: '跳转失败，请重试',
            icon: 'none',
          })
        }
      },
      complete: () => {
        this.setData({
          loadMiniProgram: false,
        })
      },
    })
  },
  // 跳转回收详情
  goXx() {
    // jump('activityDetail', {
    //   id: 1
    // })
    jump('convenientRecycle')
  },
  // 跳转回收预约
  goRecycle() {
    if (this.loading) return
    this.loading = true
    if (app.isLogin()) {
      // 判断定位权限是否打开
      if (getUserLocation()) {
        jump('recycle')
        this.loading = false
      } else {
        getUserPermissions('userLocation', (res) => {
          if (res) {
            jump('recycle')
          }
          this.loading = false
        })
      }
    } else {
      app.login()
      this.loading = false
    }
  },
  //小鹿闲置小程序跳转弹窗数据统计
  miniGoodsDataLogSave(type) {
    request.post(api.miniGoodsDataLogSave, {
      type
    }, {
      loading: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getSecondHandData()
    if (options?.inviteData) {
      setStorage('inviteData', options?.inviteData)
      console.log('分享人数据：', options.inviteData)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getSecondHandData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return shareOptions('default', e)
  },
})